So, what exactly is the kubectl command and
why is it important? kubectl is a utility used by administrators
to control Kubernetes clusters. It’s used to communicate with the Kube API
server on the control plane. This is important to users, because it allows
them to make requests to the cluster and kubectl determines which part of the control plane
to communicate with. Within a selected Kubernetes cluster, kubectl
transforms command-line entries into API calls and sends them to the Kube API server. However, to work properly, kubectl must be
configured with the location and credentials of a Kubernetes cluster. And before kubectl can be used to configure
a cluster, it must first be configured. kubectl stores its configuration in a file
in the home directory in a hidden folder named .kube, and contains the list of clusters and
the credentials that will be attached to each of those clusters. But where do the credentials come from? GKE provides them through the gcloud command. To view the configuration, either open the
config file or use the kubectl command: “config view”. Please note that the kubectl config shows
the configuration of the kubectl command itself, whereas other kubectl commands show the
configurations of cluster and workloads. For example, let’s say an administrator
wants to see a list of Pods in a cluster. After connecting kubectl to the cluster with
proper credentials, the administrator can issue the kubectl “get pod” command. kubectl then converts this command into an
API call, which it sends to the Kube API server through HTTPS on the cluster’s
control plane server. From there, the Kube API server processes
the request by querying etcd. The Kube API server then returns the results
to kubectl through HTTPS. Finally, kubectl interprets the API response
and displays the results to the administrator at the command prompt. To connect kubectl to a GKE cluster, first
retrieve the credentials for the specified cluster. This can be done with the “get-credentials”
gcloud command in any other environment where the gcloud command-line tool and
kubectl are installed. Note that both are installed by default
in Cloud Shell. By default, the gcloud “get-credentials”
command writes configuration information into a config file in the .kube directory in the
$HOME directory. If this command is rerun for a different cluster,
it’ll update the config file with the credentials for the new cluster. This configuration process only needs to be
performed once per cluster in Cloud Shell, because the .kube directory and its contents
stay in the $HOME directory. The gcloud command is how authorized users
interact with Google Cloud from the command line. If authorized, the gcloud “get-credentials”
command provides the credentials needed to connect with a GKE cluster. Although kubectl is a tool for administering
the internal state of an existing cluster, it can’t create new clusters or change the
shape of existing clusters. That’s done through the GKE control plane,
which the gcloud command and the Google Cloud console interfaces to. After the config file in the .kube folder
is configured, the kubectl command automatically references this file and connects to the default
cluster without prompting for credentials. Now let’s explore how to use
the kubectl command. kubectl’s syntax is composed of four parts:
the command, the type, the name, and optional flags. The command specifies the action that you
want to perform, such as get, describe, logs, or exec. Some commands show information, whereas others
change the cluster’s configuration. The TYPE defines the Kubernetes object that
the “command” acts upon, like Pods, deployments, nodes, or other objects, including the cluster
itself. The TYPE used in combination with “command”
tells kubectl what you want to do and the type of object you want to perform that action
on. The NAME specifies the object defined in TYPE. The name field isn’t always needed, especially
when using commands that list or show information. For example, if you run the command “kubectl
get pods” without specifying a name, the command returns the list of all Pods. To filter this list, specify a Pod’s name,
such as “kubectl get pod my-test-app” and kubectl will return only information on
the Pod named ‘my-test-app’. Some commands let you append flags to the
end, which you can think of as a way to make a special request. For example, to view the state of a Pod, use
the command “kubectl get pod my-test-app -o=yaml”. Also, it’s worth mentioning that telling
kubectl to produce a YAML output can be helpful for other tasks related to Kubernetes objects,
for example, recreating an object in another cluster. Flags can also be used to display additional
information. For example, run the command “kubectl get
pods -o=wide” to display the list of Pods in “wide” format, which reveals Pods in
the list. Wide format also displays which node each
Pod is running on. The kubectl command has many uses, from creating
Kubernetes objects, to viewing them, deleting them, and viewing or exporting configuration
files. Just remember to configure kubectl first or
to use the --kubeconfig or --context parameters, so that the commands you type are performed
on the cluster you intended.